﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace giaodien
{
    public partial class QLNhapHang : Form
    {
        
        public QLNhapHang()
        {
            InitializeComponent();
        }
        static string Ten;
        static string Ma;
        static Thread ThreadDangXuat;
        static Thread ThreadQLNhanVien;
        DataSet dssp=new DataSet();        
        ConnectData cd=new ConnectData();
        int f;
        int iFlagThemSL = 0;
        static Thread ThreadfrmLoai;
        static Thread XemLuotThemHang;
        DataTable dtml = new DataTable();

        #region chucnang
        public static void LayTen(string TenGui)
        {
            Ten = TenGui;
        }

        static void MoLaiDN()
        {
            Application.Run(new DN());
        }

        static void MoQLNhanVien()
        {
            Application.Run(new QLNhanVien());
        }

        static void MofrmLoai()
        {
            Application.Run(new frmLoai());
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            btnDangXuat_Click(null, null);
        }

        private void btnMini_Click(object sender, EventArgs e)
        {
            UI.Mini(this);
        }

        private void btnDangXuat_Click(object sender, EventArgs e)
        {
            this.Close();
            ThreadDangXuat.Start();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            UI.MouseDown(e);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            UI.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            UI.MouseUp();
        }

        private void MoLuotThemHang()
        {
            Application.Run(new XemLuotThemHang());
        }

        private void quảnLýNhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            ThreadQLNhanVien.Start();
        }
        #endregion

        #region kiemtra
        private void cbbkichco_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void txtsl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void txtdgia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
            {
                e.Handled = true;
            }
        }

        private void chknam_CheckedChanged(object sender, EventArgs e)
        {
            if (chknam.Checked)
                chknu.Checked = false;
        }

        private void chknu_CheckedChanged(object sender, EventArgs e)
        {
            if (chknu.Checked)
                chknam.Checked = false;
        }
        #endregion

        public static void LayTen(string TenGui, string MaGui)
        {
            Ten = TenGui;
            Ma = MaGui;
        }

        #region ham
        string taomatudong(string automasp)
        {
            string mln = "";
            int i = 0;
            dssp = cd.automa();
            if (dssp.Tables[0].Rows.Count > 0)
            {
                mln = dssp.Tables[0].Rows[0][0].ToString();
                i = int.Parse(mln.Substring(2, 3).ToString());
                i++;
                if (i < 10)
                    return "SP00" + i.ToString();
                else
                    return "SP0" + i.ToString();
            }
            else
            {
                return "SP001";
            }
        }

        void hienthilistview(DataTable dt)
        {
            lvwnhaphang.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem it = new ListViewItem();
                it.Text = dr["masp"].ToString();
                it.SubItems.Add(dr["tensp"].ToString());
                if ((Boolean)dr["gioitinhsp"] == true)
                    it.SubItems.Add("Nam");
                else
                    it.SubItems.Add("Nữ");
                it.SubItems.Add(dr["kichco"].ToString());
                it.SubItems.Add(dr["soluongtk"].ToString());
                it.SubItems.Add(dr["DonGia"].ToString());
                it.SubItems.Add(dr["maloai"].ToString());
                it.SubItems.Add(dr["nhacc"].ToString());
                it.Tag = int.Parse(dr["soluongtk"].ToString());
                lvwnhaphang.Items.Add(it);
            }
        }

        void trangthaibtn(Boolean b)
        {
            btnSua.Enabled = b;
            btnXoa.Enabled = b;
        }

        void trangthaitxt(Boolean t)
        {
            txttensp.ReadOnly = t;
            txtsl.ReadOnly = t;
            txtdgia.ReadOnly = t;
        }

        void trangthaicbb(Boolean cb)
        {
            cbbloaisp.Enabled = cb;
            cbbnhacc.Enabled = cb;
            cbbkichco.Enabled = cb;
        }

        void trangthaichk(Boolean c)
        {
            chknam.Enabled = c;
            chknu.Enabled = c;
        }

        void lammoi()
        {
            txtmasp.Text = "";
            txttensp.Text = "";
            txtsl.Text = "";
            txtdgia.Text = "";
            chknam.Checked = false;
            chknu.Checked = false;
            cbbkichco.Text = "";
            cbbloaisp.SelectedIndex = 0;
            cbbnhacc.SelectedIndex = -1;
        }

        int kiemtrathongtintrung()
        {
            int flag = 0;
            foreach (ListViewItem lvw in lvwnhaphang.Items)
            {
                if (txttensp.Text == lvw.SubItems[1].Text)
                    flag = 1;
            }
            return flag;
        }
        #endregion

        private void QLNhapHang_Load(object sender, EventArgs e)
        {
            trangthaibtn(false);
            btnThem.Enabled = true;
            trangthaicbb(false);
            trangthaichk(false);
            trangthaitxt(true);
            lblXinChao.Text = "Xin chào " + Ten;
            ThreadDangXuat = new Thread(new ThreadStart(MoLaiDN));
            ThreadQLNhanVien = new Thread(new ThreadStart(MoQLNhanVien));
            ThreadfrmLoai = new Thread(new ThreadStart(MofrmLoai));
            XemLuotThemHang = new Thread(new ThreadStart(MoLuotThemHang));
            dssp = cd.laydulieuDTS("select * from sanpham");
            DataTable dtncc = new DataTable();
            dtncc = cd.laydulieuBang("select distinct (nhacc) from sanpham");
            cbbnhacc.DataSource = dtncc;
            cbbnhacc.DisplayMember = "nhacc";
            cbbnhacc.SelectedIndex = -1;
            dtml = cd.laydulieuBang("select * from loai");
            cbbloaisp.DataSource = dtml;
            cbbloaisp.ValueMember = "maloai";
            cbbloaisp.DisplayMember = "tenloai";
            cbbloaisp.SelectedIndex = 0;
            hienthilistview(dssp.Tables[0]);
            lblXinChao.Text="Xin chào:"+ Ten;
        }

        private void lvwnhaphang_MouseDown(object sender, MouseEventArgs e)
        {
            btnThemSoLuong.Text = "Them";
            iFlagThemSL = 0;
            ListViewItem it = new ListViewItem();
            it = lvwnhaphang.GetItemAt(e.X, e.Y);
            trangthaitxt(true);
            if (it!=null)
            {
                trangthaibtn(true);
                trangthaichk(false);
                trangthaicbb(false);
                btnThemSoLuong.Visible = true;
                txtmasp.Text = it.Text;
                txttensp.Text = it.SubItems[1].Text;
                if (it.SubItems[2].Text == "Nam")
                    chknam.Checked = true;
                else
                    chknu.Checked = true;
                cbbkichco.Text = it.SubItems[3].Text;
                txtsl.Text = it.SubItems[4].Text;
                txtdgia.Text = it.SubItems[5].Text;
                cbbloaisp.SelectedValue = it.SubItems[6].Text;
                cbbnhacc.Text = it.SubItems[7].Text;
            }
            else {
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                btnThemSoLuong.Visible = false;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            f = 1;
            lammoi();
            txttensp.Focus();
            trangthaibtn(false);
            trangthaitxt(false);
            trangthaichk(true);
            trangthaicbb(true);
            lvwnhaphang.Enabled = false;
            txtsl.ReadOnly = false; ;
            btnThemSoLuong.Visible = false;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            f = 2;
            txttensp.Focus();
            trangthaitxt(false);
            trangthaibtn(false);
            trangthaichk(true);
            trangthaicbb(true);
            btnThemSoLuong.Visible = false;
            txtsl.ReadOnly = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (f == 1)
            {
                txtmasp.Text = taomatudong(txtmasp.Text);
                if (txttensp.Text == "" || cbbloaisp.Text == "" || cbbnhacc.Text == "" || chknam.Checked == false && chknu.Checked == false || cbbkichco.Text == "" || txtsl.Text == "" || txtdgia.Text == "")
                { MessageBox.Show("Mời bạn nhập thông tin"); }
                else
                {
                    if (kiemtrathongtintrung() != 0)
                    { MessageBox.Show("Thông tin bạn nhập bị trùng."); }
                    else
                    {
                        if (cd.themsp(txtmasp.Text, cbbnhacc.Text, txttensp.Text, chknam.Checked, cbbkichco.Text, cbbloaisp.SelectedValue.ToString(), int.Parse(txtsl.Text), decimal.Parse(txtdgia.Text)) > 0)
                        {
                            MessageBox.Show("Thêm thành công");
                            dssp = cd.laydulieuDTS("select * from sanpham");
                            trangthaitxt(true);
                            lvwnhaphang.Enabled = true;
                            btnThem.Enabled = true;
                            trangthaichk(false);
                            trangthaicbb(false);
                            lammoi();
                            hienthilistview(dssp.Tables[0]);
                        }
                        else
                            MessageBox.Show("Thêm không thành công");
                    }
                }
            }
            else
            { 
                if (txttensp.Text == "" || cbbloaisp.Text == "" || cbbnhacc.Text == "" || chknam.Checked == false && chknu.Checked == false || cbbkichco.Text == "" || txtsl.Text == "" || txtdgia.Text == "")
                { MessageBox.Show("Mời bạn nhập thông tin"); }
                else
                {
                    if (cd.suasp(txtmasp.Text, cbbnhacc.Text, txttensp.Text, chknam.Checked, cbbkichco.Text, cbbloaisp.SelectedValue.ToString(), int.Parse(txtsl.Text), decimal.Parse(txtdgia.Text)) > 0)
                    {
                        MessageBox.Show("Sửa thành công");
                        dssp = cd.laydulieuDTS("select * from sanpham");
                        trangthaitxt(true);
                        lvwnhaphang.Enabled = true;
                        btnThem.Enabled = true;
                        trangthaichk(false);
                        trangthaicbb(false);
                        lammoi();
                        hienthilistview(dssp.Tables[0]);
                    }
                    else
                        MessageBox.Show("Sửa không thành công");
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Bạn có muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                if (cd.xoasp(txtmasp.Text) > 0)
                {
                    MessageBox.Show("Xóa thành công");
                    dssp = cd.laydulieuDTS("select * from SanPham");
                    trangthaibtn(false);
                    btnThem.Enabled = true;
                    lammoi();
                    hienthilistview(dssp.Tables[0]);
                }
                else
                    MessageBox.Show("Xóa không thành công");
            }
        }

        private void cbbloaisp_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (DataRow dr in dtml.Rows)
            {
                if (dr["maloai"].ToString() == cbbloaisp.SelectedValue.ToString())
                {
                    if ((Boolean)dr["ttkichco"] == false)
                    {
                        cbbkichco.DropDownStyle = ComboBoxStyle.Simple;
                        cbbkichco.Text = "";
                    }
                    else
                        cbbkichco.DropDownStyle = ComboBoxStyle.DropDownList;
                }
            }
        }

        private void btnthemloai_Click(object sender, EventArgs e)
        {
            this.Close();
            ThreadfrmLoai.Start();
        }

        private void btnquaylai_Click(object sender, EventArgs e)
        {
            QLNhapHang_Load(sender, e);
            lammoi();
            lvwnhaphang.Enabled = true;
            btnThemSoLuong.Visible = false;
        }

        private void btnThemSoLuong_Click(object sender, EventArgs e)
        {
            if (iFlagThemSL == 0)
            {
                txtsl.ReadOnly = false;
                txtsl.Text = "";
                btnThemSoLuong.Text = "Xong";
                iFlagThemSL = 1;
                txtsl.Focus();
                return;
            }
                if (iFlagThemSL == 1)
                {
                    if (txtsl.Text == "")
                    {
                        MessageBox.Show("Vui lòng nhập số lượng hoặc chọn sản phẩm khác");
                    }
                    else
                    {
                        string query = "insert into ThemSanPham(NguoiThem,SanPham,SoLuongTHem,NgayThem) values('" + Ma + "','" + txtmasp.Text + "'," + txtsl.Text + ",'" + System.DateTime.Today.ToShortDateString() + "')";
                        if (cd.ChayHam(query) > 0)
                        {
                            int SoLuong = int.Parse(txtsl.Text) + int.Parse(lvwnhaphang.SelectedItems[0].Tag.ToString());
                            string query1 = "update SanPham set SoLuongTK=" + SoLuong + " where masp='" + txtmasp.Text + "'";
                            if (cd.ChayHam(query1) > 0)
                            {
                                MessageBox.Show("Thêm số lượng thành công thành công");
                                iFlagThemSL = 0;
                                btnThemSoLuong.Text = "Thêm";
                                ClearForm();
                                QLNhapHang_Load(null, null);
                            }
                            else
                            {
                                MessageBox.Show("Bị lỗi mã sp");
                            }
                        }
                    }
                }
        }

        private void ClearForm(){
            txtmasp.Clear();
            txtdgia.Clear();
            txtsl.Clear();
            btnThemSoLuong.Visible = false;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
            XemLuotThemHang.Start();
        }
    }
}
